package com.zxl.manager.privacy.utils.base;

import android.app.Activity;
import android.os.Bundle;

import com.umeng.message.PushAgent;
import com.zxl.manager.privacy.utils.track.MobclickAgentHelper;

/**
 * Created by Administrator on 2016/8/19.
 */
public class BaseActivity extends Activity implements BaseFragment.BackHandledInterface {

    protected BaseFragment mBackHandedFragment;

    private HomeWatcherReceiver mHomeWatcherReceiver = new HomeWatcherReceiver() {

        @Override
        public void onHomeClick() {
            BaseActivity.this.onHomeClick();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PushAgent.getInstance(this).onAppStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgentHelper.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgentHelper.onPause(this);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        mHomeWatcherReceiver.regist(this);
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHomeWatcherReceiver.unregist(this);
    }

    protected void onHomeClick() {
        if (null != mBackHandedFragment) {
            mBackHandedFragment.onHomeClick();
        }
    }

    @Override
    public void setSelectedFragment(BaseFragment selectedFragment) {
        mBackHandedFragment = selectedFragment;
    }

    public BaseFragment getCurrentFragment() {
        return mBackHandedFragment;
    }

    @Override
    public void onBackPressed() {
        if (null != mBackHandedFragment) {
            if (!mBackHandedFragment.onBackPressed()) {
                if (getFragmentManager().getBackStackEntryCount() == 0) {
                    super.onBackPressed();
                } else {
                    getFragmentManager().popBackStack();
                }
            }
        } else {
            super.onBackPressed();
        }
    }

}
